package org.buptsse.ui;

import org.buptsse.entity.SellDetailItem;
import org.buptsse.thread.GetSellDetailThread;
import org.buptsse.thread.SendMessageThread;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.GloableInstance;
import org.buptsse.utils.ImageService;

import android.R.bool;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

//here i am
public class OneBookActivity extends Activity implements OnClickListener {

	private String sellid;
	private ImageView bookImageIV,sellerPhotoIV;
	private TextView bookNameTV,sellPriceTV,selledNumberTV,isbnTV,
					 pageTV,newPriceTV,authorTV,publisherTV,publishtimeTV,describeTV,
					 conditionTV,bookNumberTV,updateTimeTV,commentTV,sellerNameTV,
					 sellerDegreeTV,sellerCoinTV,sellerSchoolTV,sellerSecondSubjectTV,
					 registtimeTV;
	private LinearLayout sellerPhotosLayout;
	private ProgressDialog pDialog;
	private SellDetailItem sellDetailItem;
	private Button backButton,refreshButton,callButton,SMSButton,messageButton;
	
	private Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case APPConstant.NetUnavailable:
				pDialog.dismiss();
				Toast.makeText(getApplicationContext(), "网络连接不可用", 1000).show();
				break;
			case APPConstant.getSellBySellidError:
				pDialog.dismiss();
				finish();
				Toast.makeText(getApplicationContext(), "该商品以删除", 1000).show();
				break;
			case APPConstant.getSellBySellidFinish:
				sellDetailItem=(SellDetailItem) msg.obj;
				initComponent();
				FillInfo(sellDetailItem);
				pDialog.dismiss();
				break;
			case APPConstant.sendMessageError:
				Toast.makeText(getApplicationContext(), "消息发送失败", 1000).show();
				pDialog.dismiss();
				break;
			case APPConstant.sendMessageFinish:
				Toast.makeText(getApplicationContext(), "操作发送成功", 1000).show();
				pDialog.dismiss();
				break;
			}
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		Intent intent=getIntent();
		sellid=intent.getStringExtra("sellid");
		super.onCreate(savedInstanceState);
		getSellDetail();
	}
	
	private void initComponent()
	{
		this.setContentView(R.layout.l_book_info);
		isbnTV=(TextView) findViewById(R.id.isbn);
		selledNumberTV=(TextView) findViewById(R.id.sellednumber);
		sellPriceTV=(TextView) findViewById(R.id.sellprice);
		bookNameTV=(TextView) findViewById(R.id.bookname);
		describeTV=(TextView) findViewById(R.id.describe);
		publishtimeTV=(TextView) findViewById(R.id.publishtime);
		publisherTV=(TextView) findViewById(R.id.publisher);
		authorTV=(TextView) findViewById(R.id.author);
		newPriceTV=(TextView) findViewById(R.id.newprice);
		pageTV=(TextView) findViewById(R.id.page_number);
		commentTV=(TextView) findViewById(R.id.comment);
		updateTimeTV=(TextView) findViewById(R.id.updatetime);
		bookNumberTV=(TextView) findViewById(R.id.booknumber);
		conditionTV=(TextView) findViewById(R.id.condition);
		sellerNameTV=(TextView) findViewById(R.id.sellername);
		registtimeTV=(TextView) findViewById(R.id.registtime);
		sellerSecondSubjectTV=(TextView) findViewById(R.id.sellersecondsubject);
		sellerSchoolTV=(TextView) findViewById(R.id.sellerschool);
		sellerCoinTV=(TextView) findViewById(R.id.sellercoin);
		sellerDegreeTV=(TextView) findViewById(R.id.sellerdegree);
		bookImageIV=(ImageView) findViewById(R.id.bookimage);
		sellerPhotoIV=(ImageView) findViewById(R.id.sellerphoto);
		sellerPhotosLayout=(LinearLayout) findViewById(R.id.photos_layout);
		
		backButton=(Button) findViewById(R.id.rd_back);
		backButton.setOnClickListener(this);
		refreshButton=(Button) findViewById(R.id.rd_refresh);
		refreshButton.setOnClickListener(this);
		/*callButton=(Button) findViewById(R.id.call_button);
		callButton.setOnClickListener(this);
		SMSButton=(Button) findViewById(R.id.sms_button);
		SMSButton.setOnClickListener(this);*/
		messageButton=(Button) findViewById(R.id.rd_message);
		messageButton.setOnClickListener(this);
	}

	public void ShowWaittingDialog(String msg)
	{
		pDialog=new ProgressDialog(this);
		pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setMessage(msg); 
        pDialog.show(); 
	}
	private Dialog dialog;
	private CheckBox makeOrderCB;
	private Button sendMessageBT,cancelSendBT;
	private EditText contentET;
	private void ShowMessageDialog(String msg)
	{
		if(dialog==null)
		{
			dialog=new Dialog(OneBookActivity.this);
			dialog.setTitle("发送站内消息");
		    WindowManager.LayoutParams windowParams = dialog.getWindow().getAttributes();
            windowParams.width = LinearLayout.LayoutParams.WRAP_CONTENT; // 设置宽度
            windowParams.height = LinearLayout.LayoutParams.WRAP_CONTENT; // 设置高度
            dialog.getWindow().setAttributes(windowParams);
			dialog.setContentView(R.layout.make_deal_layout);
			makeOrderCB=(CheckBox) dialog.findViewById(R.id.make_order);
			/*makeOrderCB.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(isChecked==true)
				}
			});*/
			contentET=(EditText) dialog.findViewById(R.id.message_content);
			sendMessageBT=(Button) dialog.findViewById(R.id.send_message);
			sendMessageBT.setOnClickListener(this);
			cancelSendBT=(Button) dialog.findViewById(R.id.send_cancel);
			cancelSendBT.setOnClickListener(this);
		}
		dialog.show();
		contentET.setText(msg);
	}
	
	private void hideDialog()
	{
		if(dialog!=null)
			dialog.hide();
	}
	
	private void getSellDetail()
	{
		GetSellDetailThread gsdt=new GetSellDetailThread(getApplicationContext(), handler, sellid);
		gsdt.start();
		ShowWaittingDialog("正在加载商品详情");
	}
	
	private void FillInfo(SellDetailItem item)
	{
		bookNameTV.setText(item.getBookname());
		sellPriceTV.setText(item.getSellprice());
		selledNumberTV.setText(item.getSellednumber());
		isbnTV.setText(item.getIsbn());
		////////////
		pageTV.setText(item.getPage());
		newPriceTV.setText(item.getNewprice());
		authorTV.setText(item.getAuthor());
		publisherTV.setText(item.getPublisher());
		publishtimeTV.setText(item.getPublishtime());
		describeTV.setText(item.getDescribe());
		//////////////
		conditionTV.setText(item.getCondition());
		bookNumberTV.setText(item.getBooknumber());
		updateTimeTV.setText(item.getUpdatetime());
		commentTV.setText(item.getComment());
		////////////////
		sellerNameTV.setText(item.getSellername());
		sellerDegreeTV.setText(item.getSellerdegree());
		sellerCoinTV.setText(item.getSellercoin());
		sellerSchoolTV.setText(item.getSellerschool());
		sellerSecondSubjectTV.setText(item.getSellersecondsubjectString());
		registtimeTV.setText(item.getRegisttime());
		///////////////
		bookImageIV.setImageBitmap(ImageService.getImageByUrl(item.getBookphoto()));
		if(item.getPhotos()!=null && !item.getPhotos().equals(";"));
		{
			String[] bookPhotos=item.getPhotos().split(";");
			for(int i=0;i<bookPhotos.length;i++)
			{
				if(bookPhotos[i]!=null && !bookPhotos[i].equals(""))
				{
					ImageView imageView=new ImageView(getApplicationContext());
					imageView.setImageBitmap(ImageService.getImageByUrl(bookPhotos[i]));
					
					LayoutParams layoutParams=new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
					//layoutParams.
					sellerPhotosLayout.addView(imageView,layoutParams);
				}
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rd_back:
			finish();
			break;
		case R.id.rd_refresh:
			getSellDetail();
			hideDialog();
			break;
		case R.id.rd_message:
			String msg="你好，我是"+GloableInstance.user.getName()+",我看到了您在《云端书房》出售的二手书《"+sellDetailItem.getBookname()+"》，我想购买次数，请尽快联系我。";
			ShowMessageDialog(msg);
			break;
		case R.id.send_message:
			String content=contentET.getText().toString();
			String makeOrder=makeOrderCB.isChecked()?"true":"false";
			SendMessageThread smt=new SendMessageThread(getApplicationContext(), handler, content, GloableInstance.user.getName(), sellDetailItem.getSellername(), makeOrder,sellid);
			smt.start();
			hideDialog();
			ShowWaittingDialog("正在发送消息");
			break;
		case R.id.send_cancel:
			hideDialog();
			break;
		}
	}
}
